package oa.hleast.xswl.controller.app.pms;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import oa.hleast.common.enums.QueryModeEnum;
import oa.hleast.common.result.Result;
import oa.hleast.common.web.util.RequestUtils;
import oa.hleast.xswl.pojo.domain.pms.PmsUserPointThing;
import oa.hleast.xswl.service.pms.PmsUserPointThingService;
import org.springframework.web.bind.annotation.*;


@Api(tags = "【移动端】")
@RestController("AppPmsUserPointThingController")
@RequestMapping("/api.app/v1/pmsUserPointThings")
@Slf4j
@AllArgsConstructor
public class PmsUserPointThingController {

    private PmsUserPointThingService pmsUserPointThingService;

    @ApiOperation(value = "列表分页", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "queryMode", value = "查询模式", paramType = "query", dataType = "QueryModeEnum", dataTypeClass = QueryModeEnum.class),
            @ApiImplicitParam(name = "page", value = "页码", paramType = "query", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "limit", value = "每页数量", paramType = "query", dataType = "Long", dataTypeClass = Long.class),
    })
    @GetMapping
    public Result list(
            String queryMode,
            Integer page,
            Integer limit) {
        QueryModeEnum queryModeEnum = QueryModeEnum.getValue(queryMode);
        switch (queryModeEnum) {
            default:
                LambdaQueryWrapper<PmsUserPointThing> queryWrapper = new LambdaQueryWrapper<PmsUserPointThing>()
                        .orderByAsc(PmsUserPointThing::getSort)
                        .orderByDesc(PmsUserPointThing::getGmtModified)
                        .orderByDesc(PmsUserPointThing::getGmtCreate);

                Page<PmsUserPointThing> result = pmsUserPointThingService.page(new Page<>(page, limit), queryWrapper);
                return Result.success(result.getRecords(), result.getTotal());
        }
    }

    @ApiOperation(value = "新增", httpMethod = "POST")
    @PostMapping("/addPointThing/{id}")
    public Result add(@PathVariable Long id) {
        Long userId = RequestUtils.getUserId();
        PmsUserPointThing pmsUserPointThing = new PmsUserPointThing();
        pmsUserPointThing.setUserId(userId);
        pmsUserPointThing.setPointThingId(id);
        Boolean status = pmsUserPointThingService.save(pmsUserPointThing);
        return Result.judge(status);
    }

    @ApiOperation(value = "详情", httpMethod = "GET")
    @ApiImplicitParam(name = "id", value = "id", required = true, paramType = "path", dataType = "Long", dataTypeClass = Long.class)
    @GetMapping("/{id}")
    public Result detail(@PathVariable Long id) {
        return Result.success(pmsUserPointThingService.getById(id));
    }

}
